package com.example.picvideodemo;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cmcy.medialib.utils.MediaSelector;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements AddImageAdapter.OnAddMediaListener, AddImageAdapter.OnItemClickListener {

    @BindView(R.id.click_cancel)
    TextView clickCancel;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.click_commit)
    TextView clickCommit;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R.id.ed_content)
    EditText edContent;
    @BindView(R.id.add_recy)
    RecyclerView addRecy;
    @BindView(R.id.rl_file)
    RelativeLayout rlFile;

    private AddImageAdapter mAddImageAdapter;
    private ArrayList<String> result = new ArrayList<>();
    private int types = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mAddImageAdapter = new AddImageAdapter(MainActivity.this, MainActivity.this);
        addRecy.setLayoutManager(new GridLayoutManager(MainActivity.this, 3, GridLayoutManager.VERTICAL, false));
//        addRecy.addItemDecoration(new SpacesItemDecoration(10));
        addRecy.setAdapter(mAddImageAdapter);
        mAddImageAdapter.setData(result, "");
        mAddImageAdapter.setOnItemClickListener(this);
    }

    @OnClick({R.id.click_cancel, R.id.click_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.click_cancel:
                break;
            case R.id.click_commit:
                ArrayList<String> data = mAddImageAdapter.getData();
                for (int i = 0; i < data.size(); i++) {
                    System.out.println(data.get(i));
                }
                Toast.makeText(this, "发布成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onaddMedia() {
        if (mAddImageAdapter.getData().size() != 0) {
            if (mAddImageAdapter.gettype().equals("image")) {
                ClickImage();
            } else {
                clickVideo();
            }
            return;
        }
        PublicDialog.setphonepop(txtTitle, MainActivity.this, new OnButtonClick() {
            @Override
            public void onName(String name) {
                if (name.equals("图片")) {
                    ClickImage();
                    return;
                } else {
                    //视频
                    clickVideo();
                }
            }
        });
    }

    //选择图片
    private void ClickImage() {
        MediaSelector.get(MainActivity.this)
                .showCamera(true)//默认显示，可以不用设置
                .setSelectMode(MediaSelector.MODE_MULTI)//默认多选
                .setMaxCount(6)//默认最多选择5张，设置单选后此设置无效
                .setMediaType(MediaSelector.PICTURE)//默认选择图片
                .setDefaultList(mAddImageAdapter.getData())//默认选中的图片/视频
                .setListener(new MediaSelector.MediaSelectorListener() {
                    @Override
                    public void onMediaResult(int type, ArrayList<String> resultList) {
                        types = type;
                        mAddImageAdapter.clear();
                        mAddImageAdapter.setData(resultList, "image");
                    }
                })
                .jump();

    }

    //选择视频
    private void clickVideo() {
        mAddImageAdapter.clear();
        MediaSelector.get(MainActivity.this)
                .showCamera(true)
                .setMaxCount(1)
                .setMediaType(MediaSelector.VIDEO)
                .setDefaultList(mAddImageAdapter.getData())
                .setListener(new MediaSelector.MediaSelectorListener() {
                    @Override
                    public void onMediaResult(int type, ArrayList<String> resultList) {
                        types = type;
                        mAddImageAdapter.clear();
                        ArrayList<String> video = new ArrayList<>();
                        video.clear();
                        video.add(resultList.get(0));
                        mAddImageAdapter.setData(video, "video");
                    }
                })
                .jump();
    }


    @Override
    public void onItemClick(String type, int position) {
        if (type.equals("image")) {
            ArrayList<String> data = mAddImageAdapter.getData();
            List<String> image = new ArrayList<>();
            image.clear();
            image.addAll(data);
            ShowImageVideoActivity.actionStart(MainActivity.this, image, position + "", "image", "phone");
            return;
        } else {
            ShowImageVideoActivity.actionStart(MainActivity.this, mAddImageAdapter.getData().get(position), "video", "", "phone");
        }
    }
}
